package edu.ustb.aur.assetpackage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import edu.ustb.aur.mapper.AssetMapper;
import edu.ustb.aur.moldpackage.AbstractMold;
import edu.ustb.aur.rightpackage.AbstractRight;
import edu.ustb.aur.utils.AbstractMoldJsonDeserializer;
import edu.ustb.aur.utils.AbstractRightJsonDeserializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ShiZhe
 * @create 2021-11-05 10:04
 */
@Service
public class AssetServiceImpl extends ServiceImpl<AssetMapper, Asset> implements AssetService {
    /**
     * 创建资产
     *
     * @param asset
     *            资产对象
     * @return true or false
     */
    @Override
    public boolean createAsset(Asset asset) {
        int i = baseMapper.insert(asset);
        return i == 1;
    }

    /**
     * 根据assetId获取asset对象
     *
     * @param assetId
     *            资产id
     * @return asset对象
     */
    @Override
    public Asset findAsset(String assetId) {
        return baseMapper.selectById(assetId);
    }

    @Override
    public String findAssetIdByAssetName(String assetName) {
        QueryWrapper<Asset> wrapper = new QueryWrapper<>();
        wrapper.eq("asset_name", assetName);
        Asset asset = baseMapper.selectOne(wrapper);
        if (asset != null) {
            return asset.getAssetId();
        }else {
            return "空";
        }
    }

    /**
     * 根据资产id查询mold 不上链
     *
     * @param assetId
     *            资产hash地址
     * @return 操作规则
     */
    @Override
    public AbstractMold findMold(String assetId) {
        QueryWrapper<Asset> wrapper = new QueryWrapper<>();
        wrapper.eq("asset_id", assetId);
        Asset asset = baseMapper.selectOne(wrapper);
        String assetMold = asset.getAssetMold();
        System.out.println(assetMold);
        // 创建Gson生成器
        GsonBuilder gsonBuilder = new GsonBuilder();
        // 用自定义的反序列化类构建生成器，形成相应的类型适配器
        gsonBuilder.registerTypeAdapter(AbstractRight.class, new AbstractRightJsonDeserializer());
        gsonBuilder.registerTypeAdapter(AbstractMold.class, new AbstractMoldJsonDeserializer());
        // 利用前面构造的Gson生成器创建一个Gson实例
        Gson gson = gsonBuilder.create();
        // 将json格式的assetMold转化为相应的AbstractMold类
        AbstractMold mold = gson.fromJson(assetMold, AbstractMold.class);
        return mold;
    }

    /**
     * 修改资产
     *
     * @param asset
     *            资产对象
     * @return true or false
     */
    @Override
    public boolean updateAsset(Asset asset) {
        int i = baseMapper.updateById(asset);
        if (i == 1) {
            System.out.println("更新成功！！！");
            return true;
        } else {
            System.out.println("更新失败！！！");
            return false;
        }
    }

    /**
     * 删除资产
     *
     * @param assetId
     *            资产链上编号
     * @return true or false
     */
    @Override
    public boolean deleteAsset(String assetId) {
        int i = baseMapper.deleteById(assetId);
        if (i == 1) {
            System.out.println("删除成功！！！");
            return true;
        } else {
            System.out.println("删除失败！！！");
            return false;
        }
    }

    @Transactional
    @Override
    public boolean findAssetVisitByAssetID(String assetId) {
        Asset asset = baseMapper.selectById(assetId);
        Integer assetLock = asset.getAssetLock();
        if (assetLock == 0) {
            return false;
        }
        asset.setAssetLock(0);
        baseMapper.updateById(asset);
        return true;
    }

    @Transactional
    @Override
    public boolean updateAssetLockByAssetID(String assetId) {
        Asset asset = baseMapper.selectById(assetId);
        asset.setAssetLock(1);
        baseMapper.updateById(asset);
        return true;
    }

}
